<?php
	// change the following paths if necessary 
	$config = dirname(__FILE__) . '/../hybridauth/config.php';
	require_once( "../hybridauth/Hybrid/Auth.php" );
	// check for erros and whatnot
	$error = "";
	
	if( isset( $_GET["error"] ) ){
		$error = '<b style="color:red">' . trim( strip_tags(  $_GET["error"] ) ) . '</b><br /><br />';
	}

	// if user select a provider to login with
		// then inlcude hybridauth config and main class
		// then try to authenticate te current user
		// finally redirect him to his profile page
	if( isset( $_GET["provider"] ) && $_GET["provider"] ):
		try{
			// create an instance for Hybridauth with the configuration file path as parameter
			$hybridauth = new Hybrid_Auth( $config );
 
			// set selected provider name 
			$provider = @ trim( strip_tags( $_GET["provider"] ) );

			// try to authenticate the selected $provider
			$adapter = $hybridauth->authenticate( $provider );

			// if okey, we will redirect to user profile page 
			$hybridauth->redirect( "check.php?provider=$provider" );
			
			if( !$hybridauth->isConnectedWith( $provider ) ){ 
			// redirect him back to login page
			header( "Location: ../login.php?error=Your are not connected to $provider or your session has expired" );
		}
		}
		
		catch( Exception $e ){
			// In case we have errors 6 or 7, then we have to use Hybrid_Provider_Adapter::logout() to 
			// let hybridauth forget all about the user so we can try to authenticate again.

			// Display the recived error, 
			// to know more please refer to Exceptions handling section on the userguide
			switch( $e->getCode() ){ 
				case 0 : $error = "Unspecified error."; break;
				case 1 : $error = "Hybriauth configuration error."; break;
				case 2 : $error = "Provider not properly configured."; break;
				case 3 : $error = "Unknown or disabled provider."; break;
				case 4 : $error = "Missing provider application credentials."; break;
				case 5 : $error = "Authentification failed. The user has canceled the authentication or the provider refused the connection."; break;
				case 6 : $error = "User profile request failed. Most likely the user is not connected to the provider and he should to authenticate again."; 
					     $adapter->logout(); 
					     break;
				case 7 : $error = "User not connected to the provider."; 
					     $adapter->logout(); 
					     break;
			} 

			// well, basically your should not display this to the end user, just give him a hint and move on..
			//$error .= "<br /><br /><b>Original error message:</b> " . $e->getMessage(); 
			//$error .= "<hr /><pre>Trace:<br />" . $e->getTraceAsString() . "</pre>";
		}
    endif;
?>

<?php
		$hybridauth = new Hybrid_Auth( $config );
		$connected_adapters_list = $hybridauth->getConnectedProviders(); 
 if(count( $connected_adapters_list ) ){
	?>
    <?php }else{ ?>
         	<div class="social"><img src="images/icon/16/facebook.png" width="16" height="16" alt="facebook">&nbsp;<a href="social/social.php?provider=Facebook">Sign-in with Facebook</a>            
            </div>
            <div class="social">
            <img src="images/icon/16/googleplus.png" width="16" height="16" alt="googleplus">&nbsp;<a href="social/social.php?provider=Google">Sign-in with Google</a>
            </div>
            <div class="social">
			<img src="images/icon/16/yahoo.png" width="16" height="16" alt="yahoo">&nbsp;<a href="social/social.php?provider=Yahoo">Sign-in with Yahoo</a>	
            </div>
            <div class="social">			
			<img src="images/icon/16/twitter.png" width="16" height="16" alt="twitter">&nbsp;<a href="social/social.php?provider=Twitter">Sign-in with Twitter</a>
            </div>
            <div class="social">
			<img src="images/icon/16/myspace.png" width="16" height="16" alt="myspace">&nbsp;<a href="social/social.php?provider=MySpace">Sign-in with MySpace</a>
            </div>
            <div class="social"> 
			<img src="images/icon/16/windowslive.png" width="16" height="16" alt="windowslive">&nbsp;<a href="social/social.php?provider=Live">Sign-in with Windows Live</a>
            </div>
            <div class="social">  
			<img src="images/icon/16/linkedin.png" width="16" height="16" alt="linkedin">&nbsp;<a href="social/social.php?provider=LinkedIn">Sign-in with LinkedIn</a>
            </div>
            <div class="social">
			<img src="images/icon/16/foursquare.png" width="16" height="16" alt="foursquare">&nbsp;<a href="social/social.php?provider=Foursquare">Sign-in with Foursquare</a>
            </div>
            <div class="social">
			<img src="images/icon/16/aol-icon.png" width="16" height="16" alt="aol-icon">&nbsp;<a href="social/social.php?provider=AOL">Sign-in with AOL</a>
            </div>
            
            
            <?php
            }
?>
<?php 
	// try to get already authenticated provider list
	try{
		$hybridauth = new Hybrid_Auth( $config );

		$connected_adapters_list = $hybridauth->getConnectedProviders(); 

		if( count( $connected_adapters_list ) ){
?> 
    <td align="left" valign="top"> 
<center>
			<?php
				foreach( $connected_adapters_list as $adapter_id ){
					?>
                    you are login with <span style="font-weight:bold;"><?php echo $adapter_id?></span>, please create account!<br/>
                    <form name="buyer_registerForm" id="buyer_registerForm" action="<?php echo W_ROOT; ?>/social/check.php?provider=<?php echo $adapter_id?>" method="post">
                    	<input type="hidden" name="account_from" value="<?php echo $adapter_id?>"/>
<div class="row">
            	<div class="six columns">
                    <label class="checkbox" class="span6">
                    <input type="radio" name="accounttype" value="3">&nbsp;Buyer&nbsp;&nbsp;&nbsp;
                    </label>
                </div>
                <div class="six columns">
                    <label class="checkbox">
                        <input type="radio" name="accounttype" value="2">&nbsp;Seller
                    </label>
                </div>
            </div>					
                    <input type="submit" value="Register" name="btnSubmit" onclick="tit();" />
    </form>  
                    <?php
				}
			?> 
</center>
	</td>		
<?php
		}
	}
	catch( Exception $e ){
		echo "Ooophs, we got an error: " . $e->getMessage();

		echo " Error code: " . $e->getCode();

		echo "<br /><br />Please try again.";

		echo "<hr /><h3>Trace</h3> <pre>" . $e->getTraceAsString() . "</pre>"; 
	}
?> 
  </tr> 
</table>
 